<script setup lang="ts">
import { ElMessage, type FormInstance, type FormRules } from 'element-plus';
import { reactive, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { loginApi } from '@/api/index'
import { useUserStore } from '@/stores/user'
import { User, Lock } from '@element-plus/icons-vue'

interface RuleForm {
    name: string,
    password: string
}

const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive<RuleForm>({
    name: '',
    password: ''
});

const rules = reactive<FormRules<RuleForm>>({
    name: [
        { required: true, message: '请填写账户名称', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请填写密码', trigger: 'blur' }
    ]
})

const router = useRouter();
const route = useRoute();
const userStore = useUserStore();

const onSubmit = async (formEl: FormInstance | undefined) => {
    if (!formEl) { return; }
    await formEl.validate((valid, fields) => {
        if (!valid) {
            ElMessage({
                message: '登录失败，请检查账户和密码是否正确',
                type: 'warning'
            })
            return;
        }

        loginApi(ruleForm.name, ruleForm.password)
            .then((res) => {
                // 更新登录状态
                userStore.setToken('{accessToken:"123456"}');

                // 跳转到上一个来源页
                let redirect = route.query.redirect?.toString();
                redirect = redirect ?? '/';
                router.push({ path: redirect });
            })
            .catch((error) => {
                //console.log('异常处理',error);
            })
            .finally(() => {
                //console.log('接口调用完成');
            });

    })
}

</script>

<template>
    <div class="login-main">
        <div class="bg"></div>
        <div class="login-box">
            <el-form :model="ruleForm" ref="ruleFormRef" :rules="rules" class="login-form">
                <el-form-item prop="name" class="form-item">
                    <el-input v-model="ruleForm.name" :prefix-icon="User" placeholder="请填写账号" />
                </el-form-item>
                <el-form-item prop="password" class="form-item">
                    <el-input v-model="ruleForm.password" :prefix-icon="Lock" placeholder="请填写密码" />
                </el-form-item>
                <el-button type="primary" @click="onSubmit(ruleFormRef)"
                    class="btn-submit">登录</el-button>
            </el-form>
        </div>
    </div>
</template>

<style lang="scss" scoped>
.login-main {
    width: 100%;
    height: 100%;
    background-color: #e5e5e5;

    .bg {
        width: 100%;
        height: 100%;
        background-color: rgb(90, 107, 207);
    }
}

.login-box {
    position: absolute;
    top: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    z-index: 2;

    .login-form {
        display: flex;
        align-items: center;
        flex-direction: column;
        width: 450px;
        padding: 40px;
        background-color: var(--ha-content-bg-color);
        border: 1px solid #e5e5e5;

        .form-item {
            width: 85%;
        }

        .btn-submit {
            width: 85%;
            height: 40px;
            border-radius: 15px;
        }
    }
}
</style>